﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using Corina.Commands;
using Corina.Lantern.DataService;

namespace Corina.Lantern.Hub.Viewmodels
{
    public class SubuserViewmodel : ViewmodelBase
    {
        public SubuserViewmodel()
        {
            UserClient.GetSubusersCompleted += GetSubusersCompleted;
            if (DataContext.IsValid)
            {
                LoadData();
            }
        }

        private void GetSubusersCompleted(object sender, GetSubusersCompletedEventArgs e)
        {
            if (e.Error == null)
            {
                if (e.Result.Result == Enumerations.ResultValue.Succeed)
                {
                    var items = (from t in e.Result.DataObjects.AsEnumerable()
                                 select new
                                 {
                                     ItemIndex = t["ROW_NUMBER"],
                                     Id = t["Id"],
                                     Name = t["Name"],
                                     Account = t["Account"],
                                     RegisterTime = t["RegisterTime"],
                                     Linkman = t["Linkman"]
                                 });
                    ItemsCollection = new ObservableCollection<object>(items);
                    ItemsCount = e.Result.TotalCount;
                }
            }
            else
            {
                MessageBox.Show(e.Error.Message);
            }
            IsBusy = false;
        }

        protected override void LoadData(params object[] paras)
        {
            IsBusy = true;
            UserClient.GetSubusersAsync(Account, Password, Index, 20);
        }

        #region Collection
        private ObservableCollection<object> _itemsCollection;
        public ObservableCollection<object> ItemsCollection
        {
            get { return _itemsCollection; }
            set
            {
                _itemsCollection = value;
                RaisePropertyChanged(() => ItemsCollection);
            }
        }
        #endregion
    }
}
